<?php
/**
 * Created by PhpStorm.
 * User: 36980
 * Date: 2017/08/12
 * Time: 14:28
 */

namespace Home\Controller;

use Think\Controller;


class UrlAnalysisController extends BaseController
{
    /***
     * 程序入口 主要是做页面展示用
     * @param string $key
     */

    public function index()
    {

        $key = $_POST['key'];
        //调用查询方法
        UrlAnalysisController::select($key);

        $this->display();
    }

    public function insert()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $this->display();
        }
        if (IS_POST) {
            //如果用户提交数据
            $model = D("Classburl");
            $spare1 = session('homeId');
			if (!trim(I('post.cbName'))) {
                    $this->error('名称不能为空');
                    exit;
                }
			if (!trim(I('post.cbURL'))) {
                    $this->error('地址不能为空');
                    exit;
                }
            //获取用户输入的信息.
            $data["cbAddTime"] = date("Y-m-d H:i:s");
            $data["cbName"] = $_POST['cbName'];
            $data["cbURL"] = $_POST['cbURL'];
            $data["cbRemarks"] = $_POST['cbRemarks'];
            if (!$model->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($model->getError());
                exit();
            } else {
                if ($cid = $model->add()) {
                    $where['cbId'] = $cid;
                    //添加条件
                    $lists = M('Classaurl')->where('caState = 0 and levelURL =0')->find();
                    $res =$data["cbURL"];
                    $shorturl = urlShort($res);
                    if (!empty($lists)) {
                        $root = $lists['caurl'] . __ROOT__ . "/f/$shorturl";
                    } else {
                        $root = $_SERVER['HTTP_HOST'] . __ROOT__ . "/f/$shorturl";
                    }
                    $data = array("cbExtension" => $root, "cbAddTime" => date("Y-m-d H:i:s"), "spare1" => $spare1,"spare2" => $shorturl);
                    $model->where($where)->setField($data);
                    $this->success("添加成功", U('UrlAnalysis/index'));
                } else {
                    $this->error("添加失败");
                }
            }
        }

    }

    public function update()
    {
        if (!IS_POST){
            $id = I('id');
            $model = M('Classburl')->where("cbId = $id")->find();
            $this->assign('post',$model);
            $this->display();
        }
        if(IS_POST){
            $model = M("Classburl");
            $id = $_POST['cbId'];
					if (!trim(I('post.cbName'))) {
                    $this->error('名称不能为空');
                    exit;
                }
			if (!trim(I('post.cbURL'))) {
                    $this->error('地址不能为空');
                    exit;
                }
            if (!$model->create()) {
                $this->error($model->getError());
            }else{

                if ($model->save()!== false) {
                    $where['cbId'] = $id;

                    $lists = M('Classaurl')->where('caState = 0 and levelURL =0')->find();
                    $res = "/Extension/jump.html?jump=$id";
                     $shorturl = urlShort($res);

                    if (!empty($lists)) {
                        $root = $lists['caurl'] . __ROOT__ . "/f/$shorturl";
                    } else {
                        $root = $_SERVER['HTTP_HOST'] . __ROOT__ . "/f/$shorturl";
                    }
                    $data = array("cbExtension" => $root, "cbUpdateTime" => date("Y-m-d H:i:s"),"spare2" => $shorturl);
                    $model->where($where)->setField($data);
                    $this->success("更新成功", U('UrlAnalysis/index'));
                } else {
                    $this->error("更新失败或填入数据没有变动");
                }
            }

        }
    }

    public function delete($id)
    {

        $model=M('Classburl')->where("cbId = $id")->delete();
        if ($model){
            $this->success("删除成功", U('UrlAnalysis/index'));
        }else{
            $this->error("删除失败");
        }

    }

    public function select($key)
    {
        $where['spare1'] = session('homeId');
        if ($key == "") {
            $model = M('classburl');
        } else {
            //模糊查询
            $where['cbName'] = array('like', '%' . trim($key) . '%');
            $model = M('classburl')->where($where);
        }
        $count = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count, 5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $member = $model->limit($Page->firstRow . ',' . $Page->listRows)->where($where)->order('cbId asc')->select();
	   // $list = M('classcurl')->limit(1)->where("ccState = 0")->order('rand()')->find();
        //二維碼
        foreach($member as $k=>$val){
            $member[$k]['qrcode'] = $this->qrcode('http://'.$val['cbextension'],$val['cbid']);
        }
        $id = session('homeId');
        $model = M('member')->find($id);
        $model['downnum'] =$model['downnum']+$model['autodailygiftnum'];
        $this->assign('result', $model);
        $this->assign('member', $member);
        $this->assign('page', $show);//
    }

	  public function  getSelectByUrl()
    {
        //获取id
        $id = I('id');
        $model = M("classburl");
        //查询一级表的内容
        $classa = M("Classaurl")->limit(1)->where("caState = 0 and levelURL =0 ")->order('rand()')->find();
        //判断是否为空
         if (!$classa) {
            $classa = M("Classaurl")->limit(1)->where("caState = 0 ")->order('rand()')->find();
            if(!$classa){
                $this->error("获取失败");
                exit;
            }

        }
        $res = "/Extension/jump.html?jump=$id";
        $shorturl = urlShort($res);
        $root = $classa['caurl'] . __ROOT__ . "/f/$shorturl";
        $data['cbExtension'] = $root;

            if ($model->where("cbId = $id")->data($data)->save() !== false) {
                $where['cbId'] = $id;
                $data = array("cbUpdateTime" => date("Y-m-d H:i:s"), "spare2" => $shorturl);
                $model->where($where)->setField($data);
                $this->redirect('UrlAnalysis/index');
            } else {
                $this->error("获取速度太快了");
            }
        }
	    public function qrcode($text='http://xx.w6cc.com',$tid,$size='4',$level='L',$padding=2,$logo=true){
        $path='./Public/png/';
        $QR=$path.$tid.'.png';
        vendor("Phpqrcode.phpqrcode");
        \QRcode::png($text,$QR, $level, $size,$padding);
        imagepng($QR);
        return $tid;
    }
}
